<template>
  <div>
    <div class="header">
      <van-icon class="back" @click="goback" size="18px" name="arrow-left" />
      <div class="title">房贷计算</div>
      <div style="width: 28px"></div>
    </div>
    <div class="space"></div>
    <div v-show="loan && active1 == 0">
      <div class="showmoney">
        <div class="box1">
          商业贷款<span>{{ businessmoney }}</span
          >万
        </div>
        <div class="box2">{{ businessyears }}年-利率{{ businessrate }}%</div>
      </div>
    </div>
    <div v-show="loan && active1 == 1">
      <div class="showmoney">
        <div class="box1">
          贷款总额<span>{{ Number(businessmoney) + Number(publicmoney) }}</span
          >万
        </div>
        <div class="box2">
          公积金贷款{{ publicmoney }}万-{{ businessyears }}年-{{
            businessrate
          }}利率
        </div>
        <div class="box2">
          商业贷款{{ businessmoney }}万-{{ publicyears }}年-{{ publicrate }}利率
        </div>
      </div>
    </div>
    <div v-show="loan && active1 == 2">
      <div class="showmoney">
        <div class="box1">
          公积金贷款<span>{{ publicmoney }}</span
          >万
        </div>
        <div class="box2">{{ publicyears }}年-{{ publicrate }}%利率</div>
      </div>
    </div>
    <div v-show="!loan && active2 == 0">
      <div class="showmoney">
        <div class="box1">
          房屋总价<span>{{ housemoney }}</span
          >万
        </div>
        <div class="box2">
          首付比例{{ firstpayproportion }}%-利率{{ businessrate }}%-{{
            businessyears
          }}年
        </div>
      </div>
    </div>
    <div v-show="!loan && active2 == 1">
      <div class="showmoney">
        <div class="box1">
          房屋总价<span>{{ housemoney }}</span
          >万
        </div>
        <div class="box2">
          公积金贷款{{ businessmoney }}万-{{ businessyears }}年-{{
            businessrate
          }}利率
        </div>
        <div class="box2">
          商业贷款{{ publicmoney }}万-{{ publicyears }}年-{{ publicrate }}利率
        </div>
      </div>
    </div>
    <div v-show="!loan && active2 == 2">
      <div class="showmoney">
        <div class="box1">
          房屋总价<span>{{ housemoney }}</span
          >万
        </div>
        <div class="box2">
          首付比例{{ firstpayproportion }}%-利率{{ publicrate }}%-{{
            publicyears
          }}年
        </div>
      </div>
    </div>
    <div class="depart">
      <div>计算方式</div>
      <div>
        <span :class="[loan ? 'active' : '']">贷款总额</span>
        <span :class="[!loan ? 'active' : '']">房屋总价</span>
      </div>
    </div>
    <div class="choosepart">
      <div
        :class="[
          (loan && active1 == 0) || (!loan && active2 == 0) ? 'active' : '',
        ]"
      >
        商业贷款
      </div>
      <div
        :class="[
          (loan && active1 == 1) || (!loan && active2 == 1) ? 'active' : '',
        ]"
      >
        组合贷款
      </div>
      <div
        :class="[
          (loan && active1 == 2) || (!loan && active2 == 2) ? 'active' : '',
        ]"
      >
        公积金贷款
      </div>
    </div>
    <div class="showus">
      <!-- <div class="top1">首付金额<span>XXXX</span>元</div> -->
      <div class="show1">
        <div>
          <div class="down"><span>等额本息</span></div>
          <div>每月应还</div>
          <div class="down">
            <span>{{ monthpay1 }}</span
            >元
          </div>
          <div>利息总额</div>
          <div class="down">
            <span>{{ interest1 }}</span
            >万
          </div>
          <div>特点</div>
          <div><span>每月月供稳定</span></div>
        </div>
        <div>
          <div class="down"><span>等额本金</span></div>
          <div>首月应还</div>
          <div class="down">
            <span>{{ monthpay2 }}</span
            >元
          </div>
          <div>利息总额</div>
          <div class="down">
            <span>{{ interest2 }}</span
            >万
          </div>
          <div>特点</div>
          <div><span>月供每月递减</span></div>
        </div>
      </div>
    </div>
    <div class="tips">*数据仅供参考</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loan: true,
      active1: 0,
      active2: 0,
      housemoney: null,
      businessmoney: null,
      businessrate: null,
      businessyears: null,
      publicmoney: null,
      publicrate: null,
      publicyears: null,
      firstpayproportion: null,
      monthpay1: null,
      interest1: null,
      monthpay2: null,
      interest2: null,
    };
  },
  created() {
    console.log(this.$route.query.sendval);
    this.loan = this.$route.query.sendval.loan;
    this.active1 = this.$route.query.sendval.active1;
    this.active2 = this.$route.query.sendval.active2;
    if (this.loan && this.active1 == 0) {
      this.businessyears = this.$route.query.sendval.businessyears;
      this.businessmoney = this.$route.query.sendval.businessmoney;
      this.businessrate = this.$route.query.sendval.businessrate;
      this.compute1();
    } else if (this.loan && this.active1 == 1) {
      this.businessyears = this.$route.query.sendval.businessyears;
      this.businessmoney = this.$route.query.sendval.businessmoney;
      this.businessrate = this.$route.query.sendval.businessrate;
      this.publicyears = this.$route.query.sendval.publicyears;
      this.publicmoney = this.$route.query.sendval.publicmoney;
      this.publicrate = this.$route.query.sendval.publicrate;
      this.compute2();
    } else if (this.loan && this.active1 == 2) {
      this.publicyears = this.$route.query.sendval.publicyears;
      this.publicmoney = this.$route.query.sendval.publicmoney;
      this.publicrate = this.$route.query.sendval.publicrate;
      this.compute3();
    } else if (!this.loan && this.active2 == 0) {
      this.housemoney = this.$route.query.sendval.housemoney;
      this.firstpayproportion = this.$route.query.sendval.firstpayproportion;
      this.businessyears = this.$route.query.sendval.businessyears;
      this.businessmoney = this.$route.query.sendval.businessmoney;
      this.businessrate = this.$route.query.sendval.businessrate;
      this.compute1();
    } else if (!this.loan && this.active2 == 1) {
      this.housemoney = this.$route.query.sendval.housemoney;
      this.firstpayproportion = this.$route.query.sendval.firstpayproportion;
      this.businessyears = this.$route.query.sendval.businessyears;
      this.businessmoney = this.$route.query.sendval.businessmoney;
      this.businessrate = this.$route.query.sendval.businessrate;
      this.publicyears = this.$route.query.sendval.publicyears;
      this.publicmoney = this.$route.query.sendval.publicmoney;
      this.publicrate = this.$route.query.sendval.publicrate;
      this.compute2();
    } else if (!this.loan && this.active2 == 2) {
      this.housemoney = this.$route.query.sendval.housemoney;
      this.firstpayproportion = this.$route.query.sendval.firstpayproportion;
      this.publicyears = this.$route.query.sendval.publicyears;
      this.publicmoney = this.$route.query.sendval.publicmoney;
      this.publicrate = this.$route.query.sendval.publicrate;
      this.compute3();
    }
  },
  methods: {
    goback() {
      this.$router.go(-1);
    },
    compute1() {
      let allmoney = this.businessmoney * 10000;
      let monthrate = (this.businessrate / 12) * 0.01;
      let allmonth = this.businessyears * 12;
      this.monthpay1 = (
        (allmoney * [monthrate * Math.pow(1 + monthrate, allmonth)]) /
        [Math.pow(1 + monthrate, allmonth) - 1]
      ).toFixed(1);
      this.interest1 = ((this.monthpay1 * allmonth - allmoney) / 10000).toFixed(
        2
      );
      this.monthpay2 = (allmoney / allmonth + allmoney * monthrate).toFixed(1);
      this.interest2 = (
        (allmonth *
          (allmoney * monthrate -
            (monthrate * (allmoney / allmonth) * (allmonth - 1)) / 2 +
            allmoney / allmonth) -
          allmoney) /
        10000
      ).toFixed(2);
    },
    compute2() {
      let allmoney = this.businessmoney * 10000;
      let monthrate = (this.businessrate / 12) * 0.01;
      let allmonth = this.businessyears * 12;
      let bm1 = (
        (allmoney * [monthrate * Math.pow(1 + monthrate, allmonth)]) /
        [Math.pow(1 + monthrate, allmonth) - 1]
      ).toFixed(1);
      let bi1 = ((bm1 * allmonth - allmoney) / 10000).toFixed(2);
      let bm2 = (allmoney / allmonth + allmoney * monthrate).toFixed(1);
      let bi2 = (
        (allmonth *
          (allmoney * monthrate -
            (monthrate * (allmoney / allmonth) * (allmonth - 1)) / 2 +
            allmoney / allmonth) -
          allmoney) /
        10000
      ).toFixed(2);

      let publicallmoney = this.publicmoney * 10000;
      let publicmonthrate = (this.publicrate / 12) * 0.01;
      let publicallmonth = this.publicyears * 12;
      let pm1 = (
        (publicallmoney *
          [publicmonthrate * Math.pow(1 + publicmonthrate, publicallmonth)]) /
        [Math.pow(1 + publicmonthrate, publicallmonth) - 1]
      ).toFixed(1);
      let pi1 = ((pm1 * publicallmonth - publicallmoney) / 10000).toFixed(2);
      let pm2 = (
        publicallmoney / publicallmonth +
        publicallmoney * publicmonthrate
      ).toFixed(1);
      let pi2 = (
        (publicallmonth *
          (publicallmoney * publicmonthrate -
            (publicmonthrate *
              (publicallmoney / publicallmonth) *
              (publicallmonth - 1)) /
              2 +
            publicallmoney / publicallmonth) -
          publicallmoney) /
        10000
      ).toFixed(2);
      this.monthpay1 = (Number(bm1) + Number(pm1)).toFixed(1);
      this.monthpay2 = (Number(bm2) + Number(pm2)).toFixed(1);
      this.interest1 = (Number(bi1) + Number(pi1)).toFixed(2);
      this.interest2 = (Number(bi2) + Number(pi2)).toFixed(2);
    },
    compute3() {
      let allmoney = this.publicmoney * 10000;
      let monthrate = (this.publicrate / 12) * 0.01;
      let allmonth = this.publicyears * 12;
      this.monthpay1 = (
        (allmoney * [monthrate * Math.pow(1 + monthrate, allmonth)]) /
        [Math.pow(1 + monthrate, allmonth) - 1]
      ).toFixed(1);
      this.interest1 = ((this.monthpay1 * allmonth - allmoney) / 10000).toFixed(
        2
      );
      this.monthpay2 = (allmoney / allmonth + allmoney * monthrate).toFixed(1);
      this.interest2 = (
        (allmonth *
          (allmoney * monthrate -
            (monthrate * (allmoney / allmonth) * (allmonth - 1)) / 2 +
            allmoney / allmonth) -
          allmoney) /
        10000
      ).toFixed(2);
    },
  },
};
</script>

<style scoped lang="less">
.header {
  position: fixed;
  left: 0;
  top: 0;
  padding: 10px 15px;
  z-index: 1000;
  background-color: #fff;
  width: 100%;
  border-bottom: 1px solid #f1f1f1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  font-size: 15px;
  color: #333;
  .title {
    line-height: 25px;
    height: 25px;
    font-size: 18px;
    font-weight: bold;
    margin-left: 10px;
  }
}
.space {
  height: 48px;
}
.showmoney {
  height: 150px;
  background: url("../../assets/buycompute01.png") no-repeat;
  background-size: 100% 100%;
  padding: 35px 15px 25px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: space-around;

  .box1 {
    font-size: 20px;
    color: #fff;

    span {
      font-size: 30px;
      margin: 0 5px;
      font-weight: bold;
    }
  }

  .box2 {
    font-size: 13px;
    color: #fff;
  }
}
.depart {
  padding: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fafafa;
  color: #333;

  > div {
    font-size: 18px;

    span {
      margin: 0 15px;
      padding-bottom: 5px;
      font-size: 16px;
      font-weight: normal;
    }

    .active {
      font-weight: bold;
      color: #45b7cb;
      border-bottom: 2px solid #45b7cb;
    }
  }
}
.choosepart {
  display: flex;
  margin: 0 15px;
  > div {
    box-sizing: border-box;
    color: #45b7cb;
    font-size: 15px;
    width: 33%;
    text-align: center;
    border: 1px solid #45b7cb;
    padding: 3px 0;
  }
  .active {
    background-color: #45b7cb;
    color: #fff;
  }
}
.showus {
  margin-top: 20px;
  background-color: #fff;
  padding: 0 15px;

  .top1 {
    font-size: 16px;
    color: #333;
    text-align: center;
    margin-bottom: 10px;
    font-weight: bold;

    span {
      font-size: 20px;
    }
  }

  .show1 {
    display: flex;
    justify-content: space-around;
    align-items: center;

    > div {
      font-size: 15px;
      color: #333;
      text-align: center;

      span {
        font-size: 17px;
        font-weight: bold;
      }

      .down {
        margin-bottom: 20px;
      }
    }
  }
}
.tips {
  font-size: 12px;
  margin: 10px 15px;
}
</style>
